package org.xtremeobjects.domain.organization;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.xtremeobjects.anotations.JavaBean;
import org.xtremeobjects.anotations.Label;
import org.xtremeobjects.anotations.association.Association;
import org.xtremeobjects.domain.party.Party;

@Entity
@JavaBean
@Label("${self.name}")
public class Organization extends Party {
	@OneToMany(mappedBy = "ownerOrganization", cascade = CascadeType.ALL, targetEntity = Organization.class)
	private List<Organization> childOrganizations = new ArrayList<Organization>();

	@ManyToOne
	private Organization ownerOrganization;
	
	public Organization addChildOrganization(String name) {
		Organization org = new Organization(name, this);
		childOrganizations.add(org);
		return org;
	}
	
	public void removeChildOrganization(Organization org) {
		childOrganizations.remove(org);
	}

	@Association(to=Organization.class)
	public List<Organization> getChildOrganizations() {
		return Collections.unmodifiableList(childOrganizations);
	}
	/**/
	
	private Organization() {
	}

	Organization(String name) {
		super(name);
		ownerOrganization = this;
	}
	
	Organization(String name, Organization owner) {
		super(name);
		ownerOrganization = owner;
	}
	
}
